package cn.codeforfun.initialization.server;

import cn.codeforfun.initialization.common.Pagination;
import cn.codeforfun.initialization.server.model.Server;
import com.jcraft.jsch.JSchException;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author wangbin
 */
@RestController
public class ServerController {
    @Resource
    private ServerService serverService;

    @PostMapping("/listServer")
    public Pagination<Server> listServer(@RequestBody Pagination<Server> pagination) {
        return serverService.listServer(pagination);
    }

    @PostMapping("/connectServer")
    public boolean connectServer(@RequestBody Server server) throws JSchException {
        return serverService.connectServer(server);
    }

    @DeleteMapping("/deleteServer/{id}")
    public void deleteServer(@PathVariable Integer id) {
        serverService.deleteServer(id);
    }

    @PostMapping("/saveServer")
    public void saveServer(@RequestBody @Valid Server server) {
        serverService.saveServer(server);
    }

    @GetMapping("/getServer/{id}")
    public Server getServerInfo(@PathVariable Integer id) {
        return serverService.getServer(id);
    }


    @GetMapping("/isServerHostExist/{host}")
    public boolean isServerHostExist(@PathVariable String host) {
        return serverService.isServerHostExist(host);
    }

    @PostMapping("/refreshServer/{id}")
    public Server refreshServer(@PathVariable Integer id) throws JSchException, JSONException {
        return serverService.refreshServer(id);
    }

    @PostMapping("/installSoft/{id}/{type}")
    public void installSoft(@PathVariable String type, @PathVariable Integer id) throws JSchException, JSONException {
        serverService.installSoft(id, type);
    }
}
